package com.nblog.controller.admin;

import com.jfinal.aop.Before;
import com.nblog.annotation.ControllerBind;
import com.nblog.constant.ApiConstant;
import com.nblog.interceptor.admin.LoginInterceptor;
import com.nblog.interceptor.admin.SysLogInterceptor;
import com.nblog.plugin.ioc.annotation.JFAutowired;
import com.nblog.resp.ApiResp;
import com.nblog.service.SysResService;
import com.nblog.view.InvokeResult;

@ControllerBind(controllerKey="/admin/res")
@Before({LoginInterceptor.class,SysLogInterceptor.class})
public class ResController extends BaseControlller {
	@JFAutowired
	SysResService sysResService;
	public void index(){
		render("index.html");
	}
	public void getData(){
		renderJson(sysResService.getTreeGridView());
	}
	public void edit(){
		render("edit.html");
	}
	public void saveRes(){
		Integer id=getParaToInt("id");
		String name=getPara("name");
		String seq=getPara("seq");
		String url=getPara("url");
		String iconCls=getPara("iconCls");
		Integer type=getParaToInt("type");
		Integer pId=getParaToInt("pid");
		if(pId!=null&&pId==0){
			pId=null;
		}
		int result=sysResService.saveRes(id, name, seq, url, iconCls, type, pId);
		ApiResp resp=new ApiResp();
		if(result==1){
			resp.setCode(ApiConstant.CODE_SUCCESS);
			resp.setMsg("添加成功！");
		}else if(result==2){
			resp.setCode(ApiConstant.CODE_SUCCESS);
			resp.setMsg("编辑成功！");
		}
		renderJson(resp);
	}
	public void setEnabled(){
		Integer resId=getParaToInt("resId");
		Integer status=getParaToInt("status");
		sysResService.setEnabled(resId, status);
		renderJson(InvokeResult.success());
	}
	public void getTree(){
		renderJson(sysResService.getTree());
	}
	public void del(){
		int id=getParaToInt("id");
		ApiResp resp=new ApiResp();
		if(sysResService.checkHasChildren(id)){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("含有子资源，先删除子资源再操作！");
			renderJson(resp);
			return;
		}
		if(sysResService.del(id)){
			resp.setCode(ApiConstant.CODE_SUCCESS);
			resp.setMsg("删除成功！");
			renderJson(resp);
		}else{
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("删除失败！");
			renderJson(resp);
		}
	}
}
